<script>
  import $ from '../dfish.js'
  import {
    h
  } from 'vue'
  import CheckBox from './CheckBox.vue'

  export default {
    name: 'w-triplebox',
    mixins: [CheckBox],
    props: ['all'],
    watch: {
      checked(a) {
        this.check(a)
        if (this.provideBoxParent) {
          this.provideBoxParent.clickBoxProxy()
        }
      }
    },
    methods: {
      getVal() {
        let s = this.checkstate()
        return s == 1 || (this.x.partialSubmit && s == 2) ? this.value : ''
      },
      formVal() {
        if (this.isDisabled) return ''
        let s = this.checkstate()
        return s == 1 || (this.x.partialSubmit && s == 2) ? this.$refs.v.value : ''
      },
      onclick(e) {
        e.stopPropagation()
        this.provideBoxParent && this.provideBoxParent.clickBoxProxy(e)
        this.relate()
      },
      checkstate(a) {
        let b = this.indeterminate ? 2 : this.isChecked ? 1 : 0
        if (a == null) {
          return b
        }
        this.x.checked = a == 1
        this.indeterminate = a == 2
      },
      isPartial() {
        return this.checkstate() == 2
      },
      getRelates() {
        return this.ownerView.fAll(this.x.name)
      },
      relate() {
        if (this.x.name) {
          for (var i = 0, b = this.getRelates(), c = this.isPartial(), l = b.length, n = l, c0 = 0, c1 = 0, c2 = 0,
              d; i < l; i++) {
            if (!b[i].isNormal) {
              n--
              continue
            }
            if (this.x.all) {
              if (this != b[i]) {
                if (c) {
                  this.checkstate(b[i].checkstate())
                  break;
                } else {
                  b[i].checkstate(this.isChecked ? 1 : 0)
                }
              } else if (l == 1)
                d = this
            } else if (b[i].x.all) {
              d = b[i]
            } else {
              b[i].isChecked ? (c1++) : b[i].isPartial() ? (c2++) : (c0++)
            }
          }
          if (d && ((c0 + c1 + c2) || l == 1)) {
            d.checkstate(c0 == n - 1 ? 0 : c1 == n - 1 ? 1 : 2)
          }
        }
      },
      check(a) {
        if (!this.isNormal) return
        let s = this.checkstate()
        if (s != a) {
          this.checkstate(a)
          this.relate()
        }
      },
    },
    mounted() {
      //!this.x.all && this.relate()
    }

  }
</script>
